<template>
  <div>
    <!-- 管理员 vshopList 修改店铺信息按钮和删除 -->
    <el-card  shadow="hover" body-style="width:100%;" style="margin-bottom: 20px">
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin-bottom: 10px"
      @click="addShop"
    >添加店铺</el-button>
    </el-card>
    <el-card  shadow="hover" body-style="width:100%;" style="margin-bottom: 20px">
    <el-table :data="shopList" style="width: 100%" border stripe :header-cell-class-name="'headerBg'" max-height="1000">
      <el-table-column prop="id" label="店铺编号" width="80px" align="center" />
      <el-table-column
        prop="userId"
        label="店主编号"
        width="80px"
        align="center"
        style="margin-left: 15px"
        fixed
      />
      <el-table-column
        prop="ownerName"
        label="店主名字"
        width="80px"
        align="center"
        style="margin-left: 15px"
      />
      <el-table-column
        prop="shopName"
        label="店铺名字"
        width="width"
        align="center"
        style="margin-left: 15px"
      />
      <el-table-column
        prop="phone"
        label="店主联系方式"
        width="120px"
        align="center"
        style="margin-left: 15px"
      />
      <el-table-column label="店铺评分" width="150" style="margin-left: 15px">
        <template slot-scope="scope">
                    <el-rate :value="scope.row.stars" disabled  text-color="#f7ad4b" :colors="['#F7AD4B', '#F7AD4B', '#F7AD4B']" />
<!--          <el-rate :value="scope.row.stars" disabled show-text text-color="#f7ad4b" :colors="['#F7AD4B', '#F7AD4B', '#F7AD4B']" />-->

        </template>
      </el-table-column>

      <el-table-column
        prop="weight"
        label="店铺权重"
        width="80px"
        align="center"
      />
      <el-table-column
        prop="isShutDown"
        label="下架该店"
        width="120px"
        align="center"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShutDown"
            active-text="下架"
            @change="updateShutDown(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="300px" fixed="right" column-align="center" align="center">
        <template slot-scope="scope">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateShop(scope.row)"
          >修改</el-button>
          <el-button
            type="danger"
            icon="el-icon-edit"
            size="mini"
            @click="deleteShop(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="shopForm.id ? '修改店铺' : '新增店铺'"
      :visible.sync="dialogFormVisible"
      width="700px"
      height="300px"
    >
      <el-form
        ref="ruleForm"
        :model="shopForm"
        style="width: 80%"
        :rules="rules"
      >
        <template v-if="this.shopForm.id">
          <!-- 如果存在id,就是修改页面,不能修改店主编号 -->
          <el-form-item label="店主编号(无法修改店主编号)" prop="userId" style="margin-left: 15px">
            <el-input v-model.number="shopForm.userId" disabled />
          </el-form-item>
        </template>
        <template v-else>
          <!-- 否则就是新增,可以输入店主编号 -->
          <el-form-item label="店主编号" prop="userId" style="margin-left: 15px">
            <el-input v-model.number="shopForm.userId" />
          </el-form-item>
        </template>

        <el-form-item label="店主名字" prop="ownerName" style="margin-left: 15px">
          <el-input v-model="shopForm.ownerName" />
        </el-form-item>
        <el-form-item label="店铺名字" prop="shopName" style="margin-left: 15px">
          <el-input v-model="shopForm.shopName" />
        </el-form-item>
        <el-form-item label="店主联系方式" prop="phone" style="margin-left: 15px">
          <el-input v-model.number="shopForm.phone" />
        </el-form-item>
        <el-form-item label="店铺评分(1-5)" prop="stars" label-width="120px" style="margin-right: 10px">
          <el-rate v-model.number="shopForm.stars" style="margin-top: 10px" :colors="['#F7AD4B', '#F7AD4B', '#F7AD4B']" />
        </el-form-item>

        <el-form-item label="店铺权重(1-10)" prop="weight" style="margin-left: 15px">
          <el-input v-model.number="shopForm.weight" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitCheckedForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页器 -->
    <el-pagination
      style="margin-top: 20px; textalign: center"
      :current-page="pageParams.page"
      :page-sizes="[3, 5, 10]"
      :page-size="pageParams.pageSize"
      :pager-count="4"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="pageParams.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'VshopList',
  data() {
    return {
      dialogFormVisible: false,
      value: null,
      shopList: [],
      // 分页参数
      pageParams: {
        page: 1,
        pageSize: 5,
        total: 0
      },
      shopForm: {
        userId: '',
        ownerName: '',
        shopName: '',
        phone: '',
        stars: '',
        weight: ''
      },

      rules: {
        userId: [
          { required: true, message: '请输入店主编号', trigger: 'blur' }
        ],
        ownerName: [
          { required: true, message: '请输入店主名字', trigger: 'blur' },
          { min: 1, message: '名字至少一个字', trigger: 'change' }
        ],
        shopName: [
          { required: true, message: '请输入店铺名字', trigger: 'blur' },
          { min: 1, message: '名字至少一个字', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入店主联系方式', trigger: 'blur' },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请输入正确的联系方式'
          }
        ],
        stars: [
          { required: true, message: '请输入店铺评分', trigger: 'blur' },
          {
            pattern: /^([1-5])$/,
            message: '评分只能是1-5的整数',
            trigger: 'change'
          }
        ],
        weight: [
          { required: true, message: '请输入店铺权重', trigger: 'blur' },
          {
            pattern: /^([1-9]|10)$/,
            message: '权重只能是1-10的整数',
            trigger: 'change'
          }
        ]
      }
    }
  },
  mounted() {
    this.getShopList()
  },
  methods: {
    async getShopList() {
      const res = await this.$API.admin.reqShopList(this.pageParams)
      if (res.code === 200) {
        this.pageParams.total = res.data.total
        this.shopList = res.data.shopVoList
      }
    },

    addShop() {
      this.dialogFormVisible = true
      this.shopForm = {
        userId: '',
        ownerName: '',
        shopName: '',
        phone: '',
        stars: '',
        weight: ''
      }
    },
    updateShop(row) {
      this.dialogFormVisible = true
      this.shopForm = { ...row }
    },
    // 删除店铺
    async deleteShop(row) {
      this.$confirm('删除店铺后后无法回撤, 是否继续?', '删除评论', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await this.$API.admin.deleteShopById(row)
          if (res.code === 200) {
            this.$message.success('删除店铺成功')
            this.getShopList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    updateShutDown(row) {
      this.shopForm = { ...row }
      this.saveOrUpdateShop()
    },
    cancel(ruleForm) {
      this.$refs[ruleForm].resetFields()
      this.dialogFormVisible = false
      this.shopForm = {
        userId: '',
        ownerName: '',
        shopName: '',
        phone: '',
        stars: '',
        weight: ''
      }
    },
    submitCheckedForm() {
      this.$refs.ruleForm.validate((success) => {
        if (success) {
          this.saveOrUpdateShop()
          // this.$message.success("验证成功,可以提交表单了");
        } else {
          this.$message({
            type: 'warning',
            message: '请输入必选项!!!'
          })
          return false
        }
      })
    },
    async saveOrUpdateShop() {
      // 修改店铺 有id
      let res
      if (this.shopForm.id) {
        res = await this.$API.admin.reqUpdateShop(this.shopForm)
      } else {
        res = await this.$API.admin.reqSaveShop(this.shopForm)
      }
      if (res.code === 444) {
        this.$message.error(res.messaage)
      } else if (res.code === 777) {
        this.$message.error(res.messaage)
      } else if (res.code === 200) {
        this.dialogFormVisible = false
        this.$message.success(
          this.shopForm.id ? '修改店铺成功' : '新增店铺成功'

        )
        this.getShopList()
      }
    },
    // 分页函数
    handleSizeChange(newPageSize) {
      this.pageParams.pageSize = newPageSize
      this.getShopList()
    },
    handleCurrentChange(current) {
      this.pageParams.page = current
      this.getShopList()
    }
  }
}
</script>

<style scoped>
.headerBg {
  background: #eee!important;
}
</style>
